import statistics as stat
line = input().split()
n, m, d = [int(x) for x in line]
nums = []
for i in range(n):
line = input().split()
int_line = [int(x) for x in line]
nums += int_line
base = nums[0] % d
def check():
for i in range(n * m):
if nums[i] % d != base:
return False
return True
if check():
steps = [(x-base) // d for x in nums]
median = stat.median(steps)
result = 0
for i in range(len(steps)):
result += abs(steps[i] - median)
result = int(result)
print(result)
else:
print(-1)
#include<bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pi pair<int, int>
#define vii vector<pi>
#define vi vector<int>
#define vvi vector<vi>
#define vpi vector<pi>
#define all(x) x.begin(),x.end()
#define print(x) cout<<(#x)<<" : "<<x<<endl;
#define pb push_back
#define sti stack<int>
#define mii map<int,int>
#define uii unordered_map<int,int>
#define uci unordered_map<char,int>
#define umsi unordered_map<string,int>
#define si set<int>
#define usi unordered_set<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define len(s) s.length()
#define w(x) int x; cin>>x; while(x--)
#define mod 1000000007
#define inf 1e18
#define fp(x,y) fixed<<setprecision(y)<<x
#define sum(v,i) accumulate(all(v),i)
#define mk(arr,n,type) type *arr=new type[n];
#define mem(a,b) memset(a, b, sizeof a)
#define iota(v,i) iota(all(v),i)
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define el "\n"
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
#define vinn(name,N) int N;cin>>N;vi name(N);rep(i,0,N){cin>>name[i];}
#define vin(name,N) vi name(N);rep(i,0,N) cin>>name[i];
#define show(V) for(auto&i:V){cout<<i<<" ";}cout<<el;
#define flush cin.ignore(numeric_limits<streamsize>::max(), '\n') // clear buffer
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rep2(i,m) for(auto &i:m)
#define rep3(i,m) for(auto i:m)
#define repr(i,a,b) for(int i=a;i>b;i--)
int max(int n1, int n2) {
return n1 > n2 ? n1 : n2;
}
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
const char* comma = strchr (names + 1, ',');
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
void fio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve() {
int n,m,d;
cin>>n>>m>>d;
int size=n*m;
vin(v,size);
sort(all(v));
rep(i,1,size){
if((v[i]-v[i-1])%d){
cout<<-1;
return;
}
}
int cnt=0;
if(size&1){
int idx=size/2;
rep(i,0,size) cnt+=abs(v[idx]-v[i])/d;
}
else{
int i1=size/2,i2=size/2-1;
int c1=0,c2=0;
rep(i,0,size) c1+=abs(v[i1]-v[i])/d;
rep(i,0,size) c2+=abs(v[i2]-v[i])/d;
cnt=min(c1,c2);
}
cout<<cnt;
}
int32_t main()
{
fio();
// clock_t z = clock();
// w(t)
solve();
// cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
return 0;
}
1173B - Nauuo and Chess | 318B - Strings of Power |
1625A - Ancient Civilization | 864A - Fair Game |
1663B - Mike's Sequence | 448A - Rewards |
1622A - Construct a Rectangle | 1620A - Equal or Not Equal |
1517A - Sum of 2050 | 620A - Professor GukiZ's Robot |
1342A - Road To Zero | 1520A - Do Not Be Distracted |
352A - Jeff and Digits | 1327A - Sum of Odd Integers |
1276A - As Simple as One and Two | 812C - Sagheer and Nubian Market |
272A - Dima and Friends | 1352C - K-th Not Divisible by n |
545C - Woodcutters | 1528B - Kavi on Pairing Duty |
339B - Xenia and Ringroad | 189A - Cut Ribbon |
1182A - Filling Shapes | 82A - Double Cola |
45A - Codecraft III | 1242A - Tile Painting |
1663E - Are You Safe | 1663D - Is it rated - 3 |
1311A - Add Odd or Subtract Even | 977F - Consecutive Subsequence |